<template>
	<div class="system-tag-container">
		<el-dialog title="修改标签" v-model="isShowDialog" >
			<el-form ref="editTagFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col>
						<el-form-item label="上级标签">
							<el-cascader
								placeholder="请选择上级标签（可选填）"
								v-model="formData.parentId"
								:options="tags"
								:props="{expandTrigger: 'hover', emitPath: false, checkStrictly: true, value: 'id', label: 'tagName', children: 'children' }"
								class="w100"
								clearable>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="标签类型" prop="tagType">
							<el-radio v-model="formData.tagType" label="catalog" clearable>目 录</el-radio>
							<el-radio v-model="formData.tagType" label="tag" clearable>标 签</el-radio>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="标签排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="标签名称" prop="tagName">
							<el-input v-model="formData.tagName" placeholder="请输入标签名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.tagType == 'catalog'">
						<el-form-item label="标签键值" prop="tagLabel">
							<el-input v-model="formData.tagLabel" placeholder="请输入标签键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.tagType == 'tag'">
						<el-form-item label="标签键值" prop="tagValue">
							<el-input v-model="formData.tagValue" placeholder="请输入标签键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.tagType == 'tag'">
						<el-form-item label="是否默认" prop="isDefault">
							<el-radio v-model="formData.isDefault" label="Y" clearable>是</el-radio>
							<el-radio v-model="formData.isDefault" label="N" clearable>否</el-radio>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="标签状态" prop="status">
							<el-radio v-model="formData.status" label="0" clearable>正常</el-radio>
							<el-radio v-model="formData.status" label="1" clearable>停用</el-radio>
							<el-radio v-model="formData.status" label="2" clearable>删除</el-radio>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getTagList, editTag } from '/@/api/system/tag/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
	name: 'tag/edit',
	setup(props, context) {
		const editTagFormRef = ref();

		const state = reactive({
			isShowDialog: false,
			tags: [],
			formData: {},
			formRules : {
				tagType: { required: true, message: '标签类型不能为空', trigger: 'change' },
				tagName: { required: true, message: '标签名称不能为空', trigger: 'blur' },
				isDefault: { required: true, message: '是否默认不能为空', trigger: 'change' },
				tagValue: { required: true, message: '标签键值不能为空', trigger: 'blur' },
				tagLabel: { required: true, message: '标签键值不能为空', trigger: 'blur' },
				status: { required: true, message: '标签状态不能为空', trigger: 'change' },
				orderNum: { required: true, message: '标签排序不能为空', trigger: 'blur' }
			}
		});
		//初始化上级部门级联
		const loadTags = async () => {
			await getTagList({tagType: 'catalog'}).then((res) => {
				if(res.code == 0){
					state.tags = lineToTreeData(res.data);
				}
			});
		}
		// 打开弹窗
		const openDialog = (row: any) => {
			state.isShowDialog = true;
			if(row){
				state.formData = Object.assign({}, row);
			}
			loadTags();
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			editTagFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 保存
		const onSubmit = () => {
			editTagFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						parentId: state.formData.parentId,
						tagName: state.formData.tagName,
						tagType: state.formData.tagType,
						tagLabel: state.formData.tagLabel,
						tagValue: state.formData.tagValue,
						isDefault: state.formData.isDefault,
						status: state.formData.status,
						orderNum: state.formData.orderNum,
						remark: state.formData.remark
					}
					editTag(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog(); // 关闭弹窗
						}
					});
				}
			});
		};
		
		return {
			editTagFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>
